package com.gxa.modules.property.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.AssertUtils;
import com.gxa.common.utils.Result;
import com.gxa.modules.property.dto.VisitorToAdd;
import com.gxa.modules.property.dto.VisitorToUpdate;
import com.gxa.modules.property.entity.VisitState;
import com.gxa.modules.property.entity.Visitor;
import com.gxa.modules.property.service.VisitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "访客管理接口")
@Slf4j
public class VisitorController {

    @Autowired
    private VisitorService visitorService;

    @ApiOperation(value = "访客添加")
    @PostMapping("/visitor/add")
    public Result add(@RequestBody VisitorToAdd visitorToAdd){
        if (visitorToAdd != null){
            this.visitorService.add(visitorToAdd);
            return new Result().ok();
        }else {
            return new Result().error();
        }
    }

    @ApiOperation(value = "访客删除")
    @DeleteMapping("/visitor/delete")
    public Result delete(@RequestParam Integer id){
        this.visitorService.delete(id);
        return new Result().ok();
    }

    @ApiOperation(value = "访问状态更新")
    @PutMapping("/visitor/update")
    public Result update(@RequestBody VisitorToUpdate visitorToUpdate){
        if (visitorToUpdate != null){
            this.visitorService.update(visitorToUpdate);
            return new Result().ok();
        }else {
            return new Result().error();
        }
    }

    @ApiOperation(value = "访客记录查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前是第几页",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少条",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "visitStateId",value = "访问状态id",dataType = "int"),
            @ApiImplicitParam(paramType = "query",name = "visitorName",value = "访客姓名",dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "communityId",value = "小区编号",dataType = "int"),

    })
    @GetMapping ("/visitor/list")
    public com.gxa.modules.property.utils.Result<List<Visitor>> queryVisitor(@RequestParam Map<String,Object> params){
        log.info("----params----{}----",params);
        AssertUtils.isBlank(String.valueOf(params),"字符串不能为空");
        com.gxa.modules.property.utils.Result<List<Visitor>> r = new com.gxa.modules.property.utils.Result<>();
        try {
            PageInfo<Visitor> pageInfo = visitorService.queryVisitorList(params);
            r = com.gxa.modules.property.utils.Result.success(pageInfo.getList(), pageInfo.getTotal());
            System.out.println(r);
        }catch (Exception e){
            e.printStackTrace();
            com.gxa.modules.property.utils.Result.failed();
        }
        return r;
    }

    @ApiOperation(value = "访客状态的下拉列表")
    @GetMapping("/visitState/list")
    public List<VisitState> getList(){
        return visitorService.list();
    }


}
